<template>
  <div class="app-container calendar-list-container">
      <!-- filter 条件查询 -->
    <div class="filter-container" style="display: flex;justify-content:flex-start">
      <el-input clearable @keyup.enter.native="handleFilter" v-model="listQuery.nickName" style="width: 200px;margin-right:15px;" class="filter-item" placeholder="昵称" >
      </el-input>
      <el-input clearable @keyup.enter.native="handleFilter" v-model="listQuery.userPhone" style="width: 200px;margin-right:15px;" class="filter-item" placeholder="手机号" >
      </el-input>
        <el-input clearable @keyup.enter.native="handleFilter" v-model="listQuery.outTradeNo" style="width: 200px;margin-right:15px;" class="filter-item" placeholder="卡号" >
      </el-input>
       <el-select placeholder='卡类型' style="width: 200px;margin-right:15px;" class="filter-item" v-model="listQuery.cardType" clearable>
        <el-option
          v-for="item in cardTypeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
       <el-input clearable @keyup.enter.native="handleFilter" v-model="listQuery.address" style="width: 200px;margin-right:15px;" class="filter-item" placeholder="所属城市" >
      </el-input>
        <el-input clearable @keyup.enter.native="handleFilter" v-model="listQuery.equipmentNumber" style="width: 200px;margin-right:15px;" class="filter-item" placeholder="所属设备编号" >
      </el-input>
        
        <br/>
      <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button  type='primary' class="filter-item" @click="handleDownload"  icon="el-icon-download"  :loading="downloadLoading">导出</el-button>
    </div>
    <!--表格 -->
    <el-table
      :data="list"
      element-loading-text="给我一点时间"
      v-loading="loading.list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column prop="index" label="序号" align="center" width="80px"/>
      <el-table-column prop="nickName" label="昵称" align="center"/>
       <el-table-column prop="userPhone" label="手机号" align="center"/>
      <el-table-column prop="cardNo" label="卡号" align="center"/>
      <el-table-column prop="time" label="可用时长" align="center"/>
      <el-table-column prop="cardTypeInfo" label="卡类型" align="center"/>
      <el-table-column prop="address" label="所属城市" align="center"/>
      <el-table-column prop="equipmentId" label="所属设备Id" align="center"/>
      <el-table-column prop="equipmentNumber" label="所属设备编号" align="center"/>
      <el-table-column prop="isUseInfo" label="是否使用" align="center"/>
      <el-table-column prop="isPastInfo" label="是否过期" align="center"/>
      <el-table-column prop="startTime" label="开始时间" align="center"/>
      <el-table-column prop="endTime" label="结束时间" align="center"/>

    
    </el-table>
    <!-- pagination 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="listQuery.offset"
        :page-sizes="[10, 20, 30, 40,100,300,500]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 回到顶部 -->
    <BackToTop></BackToTop>
  </div>
</template>
<script>
import { getBackUserCardList } from "@/api/userCard";
import { parseTime, getdTime } from "@/utils/index";

import BackToTop from "@/components/BackToTop";
export default {
  name: "user_cardList",
  components: {
    BackToTop
  },
  data() {
    return {
        // 卡类型
       cardTypeOptions:[
          {
        value:'1',
        label:'城市卡'
        },
        { 
        value:'2',
        label:'设备卡'
        }
      ],
      loading: {
        list: false
      },
      dialog: {
        view: false
      },
      total: 0,
      listQuery: {
        offset: 1,
        limit: 10,
        userId:'',
        nickName:'',
          userPhone:'',
        cardType:'',
          outTradeNo:'',
        equipmentNumber:'',
        address:'',
      },
      list: null,
      downloadLoading: false
    };
  },

  methods: {
  //导出
   //导出
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['序号','昵称','手机号','卡号','可用时长','卡类型','所属城市', '所属设备Id','所属设备编号','是否使用','是否过期','开始时间','结束时间','创建时间']
        const filterVal = [ 'index',	'nickName', 'userPhone','cardNo','time', 'cardTypeInfo', 'address','equipmentId','equipmentNumber', 'isUseInfo', 'isPastInfo','startTime','endTime','ctime']
        const data = this.formatJson(filterVal, this.list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '用户月卡列表'
        })
        this.downloadLoading = false
      })
    },
    // 数据筛选
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
          return v[j]
      }))
    },
   
    //获取列表
    async getList() {
      this.loading.list = true;
      try {
         //调用服务器接口
        const { data } = await getBackUserCardList({
          ...this.listQuery
        });
        this.list = data.records.map((item, index) => ({
          index: index + 1,
           time:item.useTimeQuantum>0?getdTime(item.useTimeQuantum/1000):"-"+getdTime(Math.abs(item.useTimeQuantum)/1000),
          cardTypeInfo:item.cardType==1?"城市卡":"设备卡",
          isUseInfo:item.isUse==1?"未使用":"使用中",
          isPastInfo:item.isPast==1?"未过期":"已过期",
          address:item.cardType==1?(item.province?item.province:'' )+(item.city?item.city:'' ):"-",
          equipmentNumber:item.cardType==2?item.equipmentNumber:"-",
          ...item
        }));
        this.total = data.total;
      } catch (error) {}
      this.loading.list = false;
    },
    //点击查找
    handleFilter() {
      this.listQuery.offset = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.offset = val;
      this.getList();
    },
  },

  created() {
    this.getList();
  }
};
</script>
<style lang="scss" scoped>
/deep/ .el-carousel__container {
  height: 500px !important;
}
</style>
